import { CheckOutlined, CopyOutlined } from "@ant-design/icons"
import { Space, message } from "antd"
import copy from "copy-to-clipboard"
import React, { useState } from "react"


interface CopyProps{
    text:string
    msg?:string
    children?:any
}

const Copy = ({text,msg,children}:CopyProps)=>{
if(!text){
return null
}    
const [isCopy,setIsCopy] = useState(false)
const onCopy = ()=>{
    copy(text,{
        debug:true,
        format:'text/plain',
        onCopy(clipboardData) {
            setIsCopy(true);
            msg?message.success(msg): message.success('复制成功:'+text)
            setTimeout(()=>{
                setIsCopy(false)
            },1000)
        },
    })
}
 if(children&&children.type){
    return React.createElement(children.type,{onClick:onCopy,...children.props})
 }
return <>
    {text}
    <a style={{marginLeft:5}} onClick={onCopy}> 
    {isCopy? <CheckOutlined />: <CopyOutlined />}
   
   
    </a>
</>
}
export default Copy